﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Management_QuestionAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            QuestionnarieDAO quest = new QuestionnarieDAO();
            dropDownQuestionarios.DataTextField = "Summary";
            dropDownQuestionarios.DataValueField = "Nrquestionnaire";
            dropDownQuestionarios.DataSource = quest.RetrieveAllQuestionnaries();
            dropDownQuestionarios.DataBind();
        }
    }
    protected void btAdicionar_Click(object sender, EventArgs e)
    {
        int qt = int.Parse(dropDownQtOpcoes.SelectedValue);
        if (ListBoxOpcoes.Items.Count < qt)
        {
            ListBoxOpcoes.Items.Add(txtOpcoesResposta.Text);
            txtOpcoesResposta.Text = string.Empty;
            ListBoxOpcoes.DataBind();
        }

    }
    protected void rbTipoMult_CheckedChanged(object sender, EventArgs e)
    {
        switch (rbTipoMult.Checked)
        {
            case true:
                dropDownQtOpcoes.Enabled = true;
                lblListaOpcoes.Visible = true;
                txtOpcoesResposta.Visible = true;
                ListBoxOpcoes.Visible = true;
                btAdicionar.Visible = true;
                btRemover.Visible = true;
                break;
            case false:
                dropDownQtOpcoes.Enabled = false;
                lblListaOpcoes.Visible = false;
                txtOpcoesResposta.Visible = false;
                ListBoxOpcoes.Visible = false;
                btAdicionar.Visible = false;
                btRemover.Visible = false;
                break;
        }
    }
    protected void rbTipoDis_CheckedChanged(object sender, EventArgs e)
    {
        switch (rbTipoDis.Checked)
        {
            case true:
                dropDownQtOpcoes.Enabled = false;
                lblListaOpcoes.Visible = false;
                txtOpcoesResposta.Visible = false;
                ListBoxOpcoes.Visible = false;
                btAdicionar.Visible = false;
                btRemover.Visible = false;
                break;
            case false:
                dropDownQtOpcoes.Enabled = true;
                lblListaOpcoes.Visible = true;
                txtOpcoesResposta.Visible = true;
                ListBoxOpcoes.Visible = true;
                btAdicionar.Visible = true;
                btRemover.Visible = true;
                break;
        }
    }
    protected void btRemover_Click(object sender, EventArgs e)
    {
        if (ListBoxOpcoes.SelectedIndex >= 0)
            ListBoxOpcoes.Items.RemoveAt(ListBoxOpcoes.SelectedIndex);
    }
    protected void btCriar_Click(object sender, EventArgs e)
    {
        Question quest = new Question();
        quest.QuestionDescription = txtQuestao.Text;

        IEnumerator enu = ListBoxOpcoes.Items.GetEnumerator();
        int count = 0;
        quest.Answer = new string[ListBoxOpcoes.Items.Count];
        while (enu.MoveNext())
        {
            quest.Answer[count] = enu.Current.ToString();
            count++;
        }

        QuestionnarieDAO questDao = new QuestionnarieDAO();
        
        int questionnarieId = int.Parse(dropDownQuestionarios.SelectedValue.ToString());
        if (rbTipoDis.Checked)
        {
            questDao.AddQuestion(quest, Question.TypeQuestion.Descriptive, questionnarieId);
        }
        else if (rbTipoMult.Checked)
        {
            questDao.AddQuestion(quest, Question.TypeQuestion.MultipleChoice, questionnarieId);
        }
        
    }
}